package com.xinyiweidai.xinyiweidai.module.bind;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.BankBean;
import com.xinyiweidai.xinyiweidai.event.BindingEvent;
import com.xinyiweidai.xinyiweidai.util.Api;
import com.xinyiweidai.xinyiweidai.util.SpUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Request;


/**
 * class:   BankActivity
 * data：   2018/9/12  12:08
 * author:  LanX
 * Description:  绑定银行卡
 */
@SuppressWarnings("ALL")
public class BankActivity extends BaseActivity {


    private EditText tv_name;
    private EditText tv_id_number;
    private EditText et_bank_number;
    private Button btn_next;
    private String name, idCard, bankNumber;


    @Override
    protected void receiveData() {
        name = SpUtil.getInstance().getString("name");
        idCard = SpUtil.getInstance().getString("idCard");
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_bank;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this, "绑定银行卡");
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_name = (EditText) findViewById(R.id.tv_name);
        tv_id_number = (EditText) findViewById(R.id.tv_id_number);
        et_bank_number = (EditText) findViewById(R.id.et_bank_number);
        btn_next = (Button) findViewById(R.id.btn_next);

        if (name.length() > 0) {
            tv_name.setText(name);
            tv_id_number.requestFocus();
        }
        if (idCard.length() > 0) {
            tv_id_number.setText(idCard);
            et_bank_number.requestFocus();
        }
    }


    @Override
    protected void addClickEvent() {
        btn_next.setOnClickListener(view -> checkData());
    }


    private void checkData() {
        name = tv_name.getText().toString();
        idCard = tv_id_number.getText().toString();
        bankNumber = et_bank_number.getText().toString();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(idCard) || TextUtils.isEmpty(bankNumber)) {
            MyToast.showError(this, "输入有误");
            return;
        }
        if (idCard.length() != 18) {
            MyToast.showError(this, "身份证有误");
        }

        Map<String, String> map = new HashMap<>();
        map.put("accountNo", bankNumber);
        map.put("idCard", idCard);
        map.put("name", name);
        map.put("userid", SpUtil.getInstance().getUserInfo().getUser_id());
        sendData(map);
    }


    private void sendData(Map<String, String> map) {
        showLoading("正在认证");
        OkHttpUtils
                .get()
                .url(Api.bindBank)
                .params(map)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        dismissLoading();
                        L.e(e.toString());
                        MyToast.showError(BankActivity.this, "网络异常");
                    }

                    @Override
                    public void onResponse(String response) {
                        L.e(response);
                        dismissLoading();
                        parseData(response);
                    }
                });
    }


    private void parseData(String response) {
        BankBean bean = new Gson().fromJson(response, BankBean.class);
        if (bean.getCode() == 1) {
            MyToast.showSuccess(BankActivity.this, "认证成功");
            EventBus.getDefault().post(new BindingEvent("status_5"));
            finish();
        } else {
            MyToast.showError(BankActivity.this, bean.getMsg());
        }
    }


}
